home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-27 | 3.5 KB | 154 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
- public class charactercollection extends text {
-
- /**
- ** Constructors
- **/
- public charactercollection(StringBuffer inString){
- super(inString);
- }
-
- public charactercollection(String inString){
- super(inString);
- }
-
-
-
- /**
- ** collection protocol stuff...
- **/
-
-
-
- /**
- ** initialvisitstate
- **/
- public visitstate initialvisitstate() {
- return new charactervisitstate(this, 1); //temporarily returning CharacterVSs,
- }
-
-
- /**
- ** isfinalvisitstate
- **/
- public boolean isfinalvisitstate(visitstate inState) {
- checkVisitStateType(inState);
- return (((textvisitstate)inState).mCurrentPosition + 1 >= stringLength());
- }
-
-
- /**
- ** succeedingvisitstate
- **/
- public visitstate succeedingvisitstate(visitstate inState) {
- checkVisitStateType(inState);
-
- if (isfinalvisitstate(inState))
- return null; // throw new collectionexception("Can't get succeeding state from a final state");
-
- ((textvisitstate)inState).mCurrentPosition ++;
- return inState;
- }
-
-
- /**
- ** elementatvisitstate
- **/
- public Object elementatvisitstate(visitstate inState) {
- checkVisitStateType(inState);
- return new Character(getNthChar(
- ((textvisitstate)inState).mCurrentPosition));
- }
-
-
- /**
- ** setelementatvisitstate
- **/
- public void setelementatvisitstate(visitstate inState, Object inElement) {
- checkVisitStateType(inState);
-
- char ch = '\u0000'; //so we don't get "maybe uninitialized" error
- boolean inputIsOK = false;
-
- if (inElement instanceof Character){
- ch = ((Character)inElement).charValue();
- inputIsOK = true;
- }
-
- if ((inElement instanceof String) && (((String)inElement).length()) == 1) {
- ch = ((String)inElement).charAt(0);
- inputIsOK = true;
- }
-
- if (! inputIsOK)
- throw new IllegalArgumentException("Incorrect value type");
-
- setNthChar(((textvisitstate)inState).mCurrentPosition, ch);
- }
-
-
- /**
- ** removevisitstate
- **/
- public void removevisitstate(visitstate inState) {
- checkVisitStateType(inState);
- int pos = ((textvisitstate)inState).mCurrentPosition;
- removeChars(pos, pos + 1);
- }
-
-
- /**
- ** insertatvisitstate
- **/
- public visitstate insertatvisitstate(visitstate inState, Object inElement, boolean inInsertAfter){
- if (!(inElement instanceof Character))
- throw new IllegalArgumentException ("Can set elements of charactercollection only to Characters");
-
- int insertionIndex = 0;
- if (inState != null) {
- checkVisitStateType(inState);
-
- insertionIndex = ((textvisitstate)inState).mCurrentPosition;
- if (inInsertAfter) {
- insertionIndex++;
- }
- }
-
- insertCharacter(insertionIndex, ((Character)inElement).charValue() );
- return inState;
- }
-
-
- /**
- ** indexatvisitstate
- **/
- public int indexatvisitstate(visitstate inState){
- checkVisitStateType(inState);
- return (((textvisitstate)inState).mCurrentPosition + 1);
- //java starts at 0, the protocol starts at 1
- }
-
-
- /**
- ** visitstateatindex
- **/
- public visitstate visitstateatindex(int inIndex){
- return new charactervisitstate (this, inIndex);
- }
-
-
- /**
- ** checkVisitStateType --protected--
- **/
- protected void checkVisitStateType(visitstate inState){
- if (! (inState instanceof charactervisitstate))
- throw new IllegalArgumentException("Expecting a charactervisitstate, not a "
- + inState.getClass());
- }
- }